//
//  JWFindBusinessAddTagVC.m
//  jinwang
//
//  Created by jzb on 2018/4/2.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWFindBusinessAddTagVC.h"
#import "JWFBFacadeVCModel.h"
#import "JWFindBusinessAddTagCell.h"


@interface JWFindBusinessAddTagVC ()

@property (nonatomic,strong) UIButton *preserveBtn;
@property (nonatomic,strong) JWFindBusinessAddTagCell *cell;

@end

@implementation JWFindBusinessAddTagVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"商家动态";
    
    UIBarButtonItem *preserveItem = [[UIBarButtonItem alloc]initWithCustomView:self.preserveBtn];
    self.navigationItem.rightBarButtonItem = preserveItem;
    
    [self setUpTableView];
    if ([self.title isEqualToString:@"商家动态"]) {
        [self makeUrl_Dynamic_moreTag];
    }else if ([self.title isEqualToString:@"身边天下"]){
        [self makeUrl_news_allTag];
    }else if ([self.title isEqualToString:@"快急商务"]){
        [self makeUrl_Business_allTag];
    }else if ([self.title isEqualToString:@"拼车运货"]){
        [self makeUrl_Freight_allTag];
    }
    
}
//配置tableView
-(void)setUpTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //注册cell
    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
    [self registerCellWithCellClassArray:@[NSStringFromClass([JWFindBusinessAddTagCell class])]];
    
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:[JWFindBusinessAddTagCell getCellReuseIdentifier]];
    self.cell.contentView.backgroundColor = [UIColor clearColor];

    for (UIView *view in self.cell.ButtonsBGView.subviews) {
        
        [view removeFromSuperview];
    }
    if (indexPath.section == 0) {
        self.cell.AddTitleLabel.text = @"已添加";
        NSInteger x = 10;
        NSInteger y = 5;
        NSInteger height = 0;
        
        for (NSInteger i=0; i<_ListmodelArr.count; i++) {
            JWFBFacadeVCModel *model = _ListmodelArr[i];
            NSString *str = model.tag_name;
//            NSString *str = arr[i];
            CGSize size = [str sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0f]}];
            
            if (x + size.width+20 > self.view.frame.size.width-20) {
                x = 10;
                y = y + size.height+15+15; //5为两行之间的高度间隔
            }
            
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, size.width+20, size.height+15)];
            [button setTitle:str forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:16.0f];
            [button setTitleColor:kRGBColor(100, 100, 100) forState:UIControlStateNormal];
            [button addTarget:self action:@selector(RemoveBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = 1000+i;
            button.layer.cornerRadius = 6;
            button.layer.masksToBounds = YES;
            button.layer.borderColor = kRGBColor(200, 200, 200).CGColor;
            button.layer.borderWidth = 1;
            [self.cell.ButtonsBGView addSubview:button];
            
            x = x + size.width+20 +10; //10为两个标签之间的宽度间隔
            height = y + size.height+15+15;
        }
        self.cell.ButtonsBGViewHeight.constant = height;

        
    }else if (indexPath.section == 1){
        
        self.cell.AddTitleLabel.text = @"点击添加更多";
        
        NSInteger x = 10;
        NSInteger y = 5;
        NSInteger height = 0;
        
        for (NSInteger i=0; i<_list.count; i++) {
            JWFBFacadeVCModel *model = _list[i];
            NSString *str = model.tag_name;
            //            NSString *str = arr[i];
            CGSize size = [str sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0f]}];
            
            if (x + size.width+20 > self.view.frame.size.width-20) {
                x = 10;
                y = y + size.height+15+15; //5为两行之间的高度间隔
            }
            
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, size.width+20, size.height+15)];
            [button setTitle:str forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:16.0f];
            [button setTitleColor:kRGBColor(100, 100, 100) forState:UIControlStateNormal];
            [button addTarget:self action:@selector(AddBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = 2000+i;
            button.layer.cornerRadius = 6;
            button.layer.masksToBounds = YES;
            button.layer.borderColor = kRGBColor(200, 200, 200).CGColor;
            button.layer.borderWidth = 1;
            [self.cell.ButtonsBGView addSubview:button];
            
            x = x + size.width+20 +10; //10为两个标签之间的宽度间隔
            height = y + size.height+15+15;
        }
        self.cell.ButtonsBGViewHeight.constant = height;
        
    }
    
    return self.cell;
}
- (void)RemoveBtnOnclick:(UIButton *)sender{
     JWFBFacadeVCModel *model = _ListmodelArr[sender.tag - 1000];
    [_ListmodelArr removeObject:model];
//    [_ListmodelArr removeObjectAtIndex:sender.tag - 1000];
    [_list addObject:model];
    [self.tableView reloadData];
    
}
- (void)AddBtnOnclick:(UIButton *)sender{
    JWFBFacadeVCModel *model = _list[sender.tag - 2000];
//    [_list removeObjectAtIndex:sender.tag - 2000];
    [_list removeObject:model];
    [_ListmodelArr addObject:model];
    [self.tableView reloadData];
    
}
#pragma mark -懒加载
- (UIButton *)preserveBtn{
    if (!_preserveBtn) {
        _preserveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _preserveBtn.frame = CGRectMake(0, 0, 44, 44);
        [_preserveBtn setTitle:@"保存" forState:UIControlStateNormal];
        [_preserveBtn addTarget:self action:@selector(preserveButOnclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _preserveBtn;
}

- (void)preserveButOnclick:(UIButton *)sender{
    
//    if ([self.title isEqualToString:@"快急商务"]) {
//        
//        [kUserDefaults setObject:_ListmodelArr forKey:@"BusinessArr"];
//        [kUserDefaults synchronize];
//        
//        [self goBack];
//    }
    
}
#pragma mark -网络请求
//商家动态获取更多标签
-(void)makeUrl_Dynamic_moreTag{
    [[DDNetworkManagerDate makeUrl_Dynamic_moreTagWithtoken:k_user_token parent_id:_parent_id] post_RequestFinshSuccess:^(id responseObject) {
        
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            _list = [NSMutableArray new];
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            //转模型
            NSMutableArray *arr = [NSMutableArray array];
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                JWFBFacadeVCModel *model = [JWFBFacadeVCModel yy_modelWithDictionary:obj];
                
                [arr addObject:model];
            }];
             self.list = arr.mutableCopy;
            for (JWFBFacadeVCModel *Listmodel in _ListmodelArr) {
                for (JWFBFacadeVCModel *model in arr) {
                    if ([Listmodel.tag_id integerValue] == [model.tag_id integerValue]) {
                        [self.list removeObject:model];
                    }
                }
            }
            
        }
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//身边天下获取更多标签
-(void)makeUrl_news_allTag{
    [[DDNetworkManagerDate makeUrl_news_allTagWithtoken:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            _list = [NSMutableArray new];
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            //转模型
            NSMutableArray *arr = [NSMutableArray array];
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                JWFBFacadeVCModel *model = [JWFBFacadeVCModel yy_modelWithDictionary:obj];
                [arr addObject:model];
            }];
            self.list = arr.mutableCopy;
            for (JWFBFacadeVCModel *Listmodel in _ListmodelArr) {
                for (JWFBFacadeVCModel *model in arr) {
                    if ([Listmodel.tag_id integerValue] == [model.tag_id integerValue]) {
                        [self.list removeObject:model];
                    }
                }
            }
        }
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//快急商务获取更多标签
-(void)makeUrl_Business_allTag{
    [[DDNetworkManagerDate makeUrl_Business_allTagWithtoken:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            _list = [NSMutableArray new];
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            //转模型
            NSMutableArray *arr = [NSMutableArray array];
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                JWFBFacadeVCModel *model = [JWFBFacadeVCModel yy_modelWithDictionary:obj];
                [arr addObject:model];
            }];
            self.list = arr.mutableCopy;
            for (JWFBFacadeVCModel *Listmodel in _ListmodelArr) {
                for (JWFBFacadeVCModel *model in arr) {
                    if ([Listmodel.tag_id integerValue] == [model.tag_id integerValue]) {
                        [self.list removeObject:model];
                    }
                }
            }
        }
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//拼车运货获取更多标签
-(void)makeUrl_Freight_allTag{
    [[DDNetworkManagerDate makeUrl_Freight_allTagWithtoken:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            _list = [NSMutableArray new];
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            //转模型
            NSMutableArray *arr = [NSMutableArray array];
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                JWFBFacadeVCModel *model = [JWFBFacadeVCModel yy_modelWithDictionary:obj];
                [arr addObject:model];
            }];
            self.list = arr.mutableCopy;
            for (JWFBFacadeVCModel *Listmodel in _ListmodelArr) {
                for (JWFBFacadeVCModel *model in arr) {
                    if ([Listmodel.tag_id integerValue] == [model.tag_id integerValue]) {
                        [self.list removeObject:model];
                    }
                }
            }
        }
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        
        [self showBottomMessag:@"网络请求失败"];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
